namespace Warehouse

open System
open System.Windows.Data
open FSharp.Windows
open Warehouse.Controls

type ProductEvents =
    | Save of (unit -> unit)
    | SizeOrColorChanging of EventWithValueAndCancel<string>
    | AddUpc
    | CalculateShippingCost
    | CalculateMinRetailCost
    | UnitChanged
    | SetAsSKU

type ProductView() as this = 
    inherit View<ProductEvents, ProductModel, ProductWindow>()

    //Lookups
    let brands = this.Control.Brand
    let productNames = this.Control.ProductName
    let units = this.Control.Units

    let sizeOrColor = this.Control.SizeOrColor
    let shopCost = this.Control.ShopCost
    let shipping = this.Control.Shipping
    let minRetailCost = this.Control.MinRetailCost
    let sku = this.Control.SKU
    let upcs = this.Control.UPCs

    do
        brands.DisplayMemberPath <- "Name"
        brands.SynchronizedWithCurrentItem()
        productNames.DisplayMemberPath <- "Name"
        units.DisplayMemberPath <- "Name"
        sku.EnsureInt()

        for tb in [ shopCost; shipping; minRetailCost; ] do tb.EnsurePositiveDecimal()

        this.CancelButton <- this.Control.Cancel

    override this.EventStreams = 
        [
            this.Control.Save.ClickToValue(Save this.OK)
            this.Control.AddUpc.ClickToValue AddUpc
            sizeOrColor.PreviewTextInput |> Event.mapToValueAndCancel SizeOrColorChanging (fun event -> sizeOrColor.Text + event.Text) 
            this.Control.CalculateShippingCost.ClickToValue CalculateShippingCost
            (shopCost.TextChanged, shipping.TextChanged) ||> Observable.merge |> Observable.mapTo CalculateMinRetailCost
            this.Control.Units.SelectionChanged |> Observable.mapTo UnitChanged
            this.Control.SetAsSKU.ClickToValue SetAsSKU
        ]

    override this.SetBindings model = 
        Binding.FromExpression
            <@
                this.Control.Title  <- model.EditingMode

                brands.ItemsSource <- model.Brands
                brands.SelectedItem <- model.SelectedBrand
                productNames.ItemsSource <- model.Brands.CurrentItem.ProductNames
                productNames.SelectedItem <- model.ProductName

                sizeOrColor.Text <- model.SizeOrColor
                sizeOrColor.IsEnabled <- model.IsSizeOrColorEnable

                units.ItemsSource <- model.UnitLookup
                units.SelectedItem <- model.Unit

                this.Control.CalculateShippingCost.IsEnabled <- model.IsCalculateShippingCostEnable
                minRetailCost.Text <- string model.MinRetailCost

                sku.Text <- string model.SKU
                upcs.ItemsSource <- model.UPCs
                upcs.SelectedItem <- model.SelectedUpc
                this.Control.AddUpc.IsEnabled <- not model.IsNewEntity
                this.Control.SetAsSKU.IsEnabled <- model.IsSetAsSKUEnable

                //for existing model to prevent override when bind lookups
                //brands.Text <- model.SelectedBrand
                productNames.Text <- model.SelectedProductName
            @>

        Binding.UpdateSourceOnChange
            <@
                shopCost.Text <- string model.ShopCost
                shipping.Text <- string model.ShippingCost
            @>

        this.Control.NoteColumn.DisplayMemberPath <- "Description"
        this.Control.NoteColumn.ItemsSource <- model.UpcNotes
